#include "Precomp.h"
#include "LotusEditor.h"
#include <boost/bind.hpp>
#include <MyGUI/MyGUI.h>

using namespace Ogre;
using namespace Lotus;

Editor::Editor()
{
}

Editor::~Editor()
{
}

void Editor::initialise()
{
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("Plugins/EditorRes","FileSystem","LotusEditorResource",false);
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("LotusEditorResource");
	editorConnection=Lotus::Game::getInstance().ActivateEditorSignal.connect(boost::bind(&Editor::activate,this));	
}

void Editor::shutdown()
{
	editorConnection.disconnect();
}

void Editor::activate()
{
	//hijack input
	Lotus::InputManager::getInstance().getMouse()->setEventCallback(this);
	Lotus::InputManager::getInstance().getKeyboard()->setEventCallback(this);
	//Load GUI
	unsigned int width=GraphicManager::getInstance().getOgreRenderWindow()->getWidth();
	unsigned int height=GraphicManager::getInstance().getOgreRenderWindow()->getHeight();
	MyGUI::Widget* root=MyGUI::Gui::getInstance().createWidget<MyGUI::Widget>("Widget",0,0,width,height,MyGUI::Align::Default,"Main");
	MyGUI::LayoutManager::getInstance().loadLayout("Editor.layout","",root,"LotusEditorResource");
	//MyGUI::LayoutManager::getInstance().loadLayout("FileBrowser.layout","",root,"LotusEditorResource");
	//Create camera
}

bool Editor::keyPressed( const OIS::KeyEvent &arg )
{
	MyGUI::Gui::getInstance().injectKeyPress(arg);
	KeyPressedEvent(arg);
	return true;
}

bool Editor::keyReleased( const OIS::KeyEvent &arg )
{
	MyGUI::Gui::getInstance().injectKeyRelease(arg);
	KeyReleasedEvent(arg);
	return true;
}

bool Editor::mouseMoved( const OIS::MouseEvent &arg )
{
	MyGUI::Gui::getInstance().injectMouseMove(arg);
	MouseMovedEvent(arg);
	return true;
}

bool Editor::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	MyGUI::Gui::getInstance().injectMousePress(arg,id);
	MousePressedEvent(arg,id);
	return true;
}

bool Editor:: mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	MyGUI::Gui::getInstance().injectMouseRelease(arg,id);
	MouseReleasedEvent(arg,id);
	return true;
}